package com.springbootshiyanyi.demo.controller;

import com.springbootshiyanyi.demo.entity.Student;
import com.springbootshiyanyi.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/students")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @GetMapping
    public List<Student> getAllStudents() {
        return studentService.findAllStudents();
    }
    @PostMapping
    public Student createStudent(@RequestBody Student student) {
        return studentService.createStudent(student);
    }
    @GetMapping("/{id}")
    public ResponseEntity<Student> getStudentById(@PathVariable Long id) {
        Student student = studentService.getStudentById(id);
        return student != null ? ResponseEntity.ok(student) : ResponseEntity.notFound().build();
    }
    @PutMapping("/{id}")
    public String updateStudent(@PathVariable Long id, @RequestBody Student updatedStudent) {
        Student student = studentService.updateStudent(id, updatedStudent);
        if (student != null) {
            ResponseEntity.ok(student);
        } else {
            ResponseEntity.notFound().build();
        }
        return "更新成功";
    }
    @DeleteMapping("/{id}")
    public String deleteStudent(@PathVariable Long id) {
        if (studentService.deleteStudent(id)) {
            ResponseEntity.noContent().build();
        } else {
            ResponseEntity.notFound().build();
        }
        return "删除成功";}
}


